﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pawn
{
    public partial class FrmDSPhieu : Form
    {
        DataClassesDataContext db = new DataClassesDataContext();
        int rowId=0;
        DataSet.PhieuVayDataTable dt;
        public FrmDSPhieu()
        {
            InitializeComponent();
            dpNgayBatDau.Value = DateTime.Now.AddMonths(-2);
            cbLoaiPhieu.SelectedIndex = 0;
        }

        private void FrmDSPhieu_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void cbLoaiPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            timer1.Start();
                  
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            dt=new DataSet.PhieuVayDataTable();
            db = new DataClassesDataContext();
            timer1.Stop();
            
            var q = from p in db.PhieuCams
                    where (p.Khach.HoTen.Contains(tbTim.Text) ||
                    p.So.Contains(tbTim.Text) ||
                    p.Khach.LienLac.Contains(tbTim.Text) ||
                    p.Khach.DiaChi.Contains(tbTim.Text) ||
                    p.Khach.CMND.Contains(tbTim.Text)) && (p.NgayLap >= dpNgayBatDau.Value && p.NgayLap <= dpNgayKetThuc.Value.Date.AddDays(1))
                    select p;
            foreach (var _p in q)
            {
                
                
                if (cbLoaiPhieu.SelectedIndex == 0 && (bool)_p.DaTraTien) continue;
                else if (cbLoaiPhieu.SelectedIndex == 2 && !(bool)_p.DaTraTien) continue;
                
                bool chuadong = true;
                Int64 tienno = 0;
                DateTime ngaydong = ((DateTime) _p.NgayLap).Date;
                DateTime hantra = ngaydong;
                var dl = from d in db.DongLais
                         where d.SoPC == _p.So
                         orderby d.NgayDong descending
                         select d;
                foreach (var d in dl)
                {
                    if (hantra == ngaydong) hantra = ((DateTime)d.NgayDong).Date;
                    
                    if (d.TienDong != null && d.TienDong!=0)
                    {
                        chuadong = false;
                        tienno =(Int64) d.TienNo;
                        ngaydong = ((DateTime)d.NgayDong).Date.AddDays(1);
                        break;
                    }
                }
                if (cbLoaiPhieu.SelectedIndex == 1 && hantra > DateTime.Now) continue;
                int songayno = (DateTime.Now.Date.AddDays(1) - ngaydong.Date).Days;
                if (songayno > 0)
                {
                    if (chuadong) tienno = (Int64)_p.TienVay * (Int64)_p.LaiSuat * songayno / 1000000;
                    else tienno += (Int64)_p.TienVay * (Int64)_p.LaiSuat * songayno / 1000000;
                }
                dt.Rows.Add(_p.So,_p.Khach.HoTen,((DateTime)_p.NgayLap).ToString("dd/MM/yyyy"),_p.TienVay,tienno,songayno,_p.DoCam,(bool)_p.DaTraTien?((DateTime)_p.NgayTra).ToString("dd/MM/yyyy"):"Chưa trả");
            }
            dgvPhieu.DataSource = dt;
            foreach (DataGridViewRow r in dgvPhieu.Rows) r.Selected = false;
            
            if(dgvPhieu.Rows.Count>rowId) dgvPhieu.Rows[rowId].Selected = true;
        }

        private void tbTim_TextChanged(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            if (dgvPhieu.SelectedRows.Count < 1) return;
            rowId = dgvPhieu.SelectedRows[0].Index;
            FrmPhieuVay frm = new FrmPhieuVay();
            frm.SuaPhieu = true;
            frm.SoPhieu = dgvPhieu.SelectedRows[0].Cells["SoPhieu"].Value.ToString();
            frm.ShowDialog();
            timer1.Start();
        }

        private void dgvPhieu_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnChiTiet_Click(sender, e);
        }

        private void dpNgayBatDau_ValueChanged(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void dpNgayKetThuc_ValueChanged(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvPhieu.SelectedRows.Count < 1) return;
            if (MessageBox.Show("Bạn chắc chắn muốn xóa phiếu này?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No) return;
            var q = from p in db.PhieuCams
                    where p.So == dgvPhieu.SelectedRows[0].Cells["SoPhieu"].Value.ToString()
                    select p;
            foreach (var p in q)
            {
                db.DongLais.DeleteAllOnSubmit(p.DongLais);
                db.PhieuCams.DeleteOnSubmit(p);
            }
            db.SubmitChanges();
            timer1.Start();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            FrmInDSPhieu frm = new FrmInDSPhieu();
            frm.dt = dt;
            frm.TenPhieu = "DANH SÁCH PHIẾU VAY";
            frm.TuNgay = dpNgayBatDau.Value;
            frm.DenNgay = dpNgayKetThuc.Value;
            frm.NgayThongKe = DateTime.Now;
            frm.ShowDialog();
        }
    }
}
